package com.itheima.service;

import com.itheima.dao.UserMapper;
import com.itheima.pojo.User;
import com.itheima.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

/**
 * @author Xiao
 * @date 2021/9/3
 */
public class UserService {
    public User login(String username, String password) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.login(username, password);
        sqlSession.close();
        return user;
    }

    public boolean register(User user) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.selectByUname(user.getUsername());
        boolean flag = user1 == null;
        if (flag) {
            mapper.add(user);
            sqlSession.commit();
        }
        sqlSession.close();
        return flag;
    }

    public User selectByUname(String username) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectByUname(username);
        sqlSession.close();
        return user;
    }
}
